home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / DEMOS / TEXCYL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-26  |  5.3 KB  |  248 lines

  1. /* $Id: texcyl.c,v 3.1 1998/06/23 03:16:51 brianp Exp $ */
  2.  
  3. /*
  4.  * Textured cylinder demo: lighting, texturing, reflection mapping.
  5.  * Brian Paul  May 1997  This program is in the public domain.
  6.  */
  7.  
  8. /*
  9.  * $Log: texcyl.c,v $
  10.  * Revision 3.1  1998/06/23 03:16:51  brianp
  11.  * added Point/Linear sampling menu items
  12.  *
  13.  * Revision 3.0  1998/02/14 18:42:29  brianp
  14.  * initial rev
  15.  *
  16.  */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <GL/glut.h>
  23.  
  24. #include "../util/readtex.c"   /* I know, this is a hack. */
  25.  
  26.  
  27. #define LIT 1
  28. #define TEXTURED 2
  29. #define REFLECT 3
  30. #define ANIMATE 10
  31. #define POINT_FILTER 20
  32. #define LINEAR_FILTER 21
  33. #define QUIT 100
  34.  
  35. static GLuint CylinderObj = 0;
  36. static GLboolean Animate = GL_TRUE;
  37.  
  38. static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
  39. static GLfloat DXrot = 1.0, DYrot = 2.5;
  40.  
  41.  
  42. static void Idle( void )
  43. {
  44.    if (Animate) {
  45.       Xrot += DXrot;
  46.       Yrot += DYrot;
  47.       glutPostRedisplay();
  48.    }
  49. }
  50.  
  51.  
  52. static void Display( void )
  53. {
  54.    glClear( GL_COLOR_BUFFER_BIT );
  55.  
  56.    glPushMatrix();
  57.    glRotatef(Xrot, 1.0, 0.0, 0.0);
  58.    glRotatef(Yrot, 0.0, 1.0, 0.0);
  59.    glRotatef(Zrot, 0.0, 0.0, 1.0);
  60.    glScalef(5.0, 5.0, 5.0);
  61.    glCallList(CylinderObj);
  62.  
  63.    glPopMatrix();
  64.  
  65.    glutSwapBuffers();
  66. }
  67.  
  68.  
  69. static void Reshape( int width, int height )
  70. {
  71.    glViewport( 0, 0, width, height );
  72.    glMatrixMode( GL_PROJECTION );
  73.    glLoadIdentity();
  74.    glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  75.    glMatrixMode( GL_MODELVIEW );
  76.    glLoadIdentity();
  77.    glTranslatef( 0.0, 0.0, -70.0 );
  78. }
  79.  
  80.  
  81. static void SetMode(GLuint m)
  82. {
  83.    /* disable everything */
  84.    glDisable(GL_LIGHTING);
  85.    glDisable(GL_TEXTURE_2D);
  86.    glDisable(GL_TEXTURE_GEN_S);
  87.    glDisable(GL_TEXTURE_GEN_T);
  88.  
  89.    /* enable what's needed */
  90.    if (m==LIT) {
  91.       glEnable(GL_LIGHTING);
  92.    }
  93.    else if (m==TEXTURED) {
  94.       glEnable(GL_TEXTURE_2D);
  95.    }
  96.    else if (m==REFLECT) {
  97.       glEnable(GL_TEXTURE_2D);
  98.       glEnable(GL_TEXTURE_GEN_S);
  99.       glEnable(GL_TEXTURE_GEN_T);
  100.    }
  101. }
  102.  
  103.  
  104. static void ModeMenu(int entry)
  105. {
  106.    if (entry==ANIMATE) {
  107.       Animate = !Animate;
  108.    }
  109.    else if (entry==POINT_FILTER) {
  110.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  111.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  112.    }
  113.    else if (entry==LINEAR_FILTER) {
  114.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  115.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  116.    }
  117.    else if (entry==QUIT) {
  118.       exit(0);
  119.    }
  120.    else {
  121.       SetMode(entry);
  122.    }
  123.    glutPostRedisplay();
  124. }
  125.  
  126.  
  127. static void Key( unsigned char key, int x, int y )
  128. {
  129.    switch (key) {
  130.       case 27:
  131.          exit(0);
  132.          break;
  133.    }
  134.    glutPostRedisplay();
  135. }
  136.  
  137.  
  138. static void SpecialKey( int key, int x, int y )
  139. {
  140.    float step = 3.0;
  141.  
  142.    switch (key) {
  143.       case GLUT_KEY_UP:
  144.          Xrot += step;
  145.          break;
  146.       case GLUT_KEY_DOWN:
  147.          Xrot -= step;
  148.          break;
  149.       case GLUT_KEY_LEFT:
  150.          Yrot += step;
  151.          break;
  152.       case GLUT_KEY_RIGHT:
  153.          Yrot -= step;
  154.          break;
  155.    }
  156.    glutPostRedisplay();
  157. }
  158.  
  159.  
  160. static void Init( void )
  161. {
  162.    GLUquadricObj *q = gluNewQuadric();
  163.    CylinderObj = glGenLists(1);
  164.    glNewList(CylinderObj, GL_COMPILE);
  165.  
  166.    glTranslatef(0.0, 0.0, -1.0);
  167.  
  168.    /* cylinder */
  169.    gluQuadricNormals(q, GL_SMOOTH);
  170.    gluQuadricTexture(q, GL_TRUE);
  171.    gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
  172.  
  173.    /* end cap */
  174.    glTranslatef(0.0, 0.0, 2.0);
  175.    gluDisk(q, 0.0, 0.6, 24, 1);
  176.  
  177.    /* other end cap */
  178.    glTranslatef(0.0, 0.0, -2.0);
  179.    gluQuadricOrientation(q, GLU_INSIDE);
  180.    gluDisk(q, 0.0, 0.6, 24, 1);
  181.  
  182.    glEndList();
  183.    gluDeleteQuadric(q);
  184.  
  185.    /* lighting */
  186.    glEnable(GL_LIGHTING);
  187.    {
  188.       GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
  189.       GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
  190.       GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
  191.       glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
  192.       glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
  193.       glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
  194.       glEnable(GL_LIGHT0);
  195.    }
  196.  
  197.    /* fitering = nearest, initially */
  198.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  199.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  200.  
  201.    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  202.    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  203.  
  204.    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  205.    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  206.  
  207.    if (!LoadRGBMipmaps("reflect.rgb", GL_RGB)) {
  208.       printf("Error: couldn't load texture image\n");
  209.       exit(1);
  210.    }
  211.  
  212.    glEnable(GL_CULL_FACE);  /* don't need Z testing for convex objects */
  213.  
  214.    SetMode(LIT);
  215. }
  216.  
  217.  
  218. int main( int argc, char *argv[] )
  219. {
  220.    glutInit( &argc, argv );
  221.    glutInitWindowSize( 400, 400 );
  222.  
  223.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  224.  
  225.    glutCreateWindow(argv[0] );
  226.  
  227.    Init();
  228.  
  229.    glutReshapeFunc( Reshape );
  230.    glutKeyboardFunc( Key );
  231.    glutSpecialFunc( SpecialKey );
  232.    glutDisplayFunc( Display );
  233.    glutIdleFunc( Idle );
  234.  
  235.    glutCreateMenu(ModeMenu);
  236.    glutAddMenuEntry("Lit", LIT);
  237.    glutAddMenuEntry("Textured", TEXTURED);
  238.    glutAddMenuEntry("Reflect", REFLECT);
  239.    glutAddMenuEntry("Point Filtered", POINT_FILTER);
  240.    glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
  241.    glutAddMenuEntry("Toggle Animation", ANIMATE);
  242.    glutAddMenuEntry("Quit", QUIT);
  243.    glutAttachMenu(GLUT_RIGHT_BUTTON);
  244.  
  245.    glutMainLoop();
  246.    return 0;
  247. }
  248.